package com.ourcraft.informationcollectionsystem.controller.province;

import com.alibaba.fastjson.JSONObject;
import com.ourcraft.informationcollectionsystem.entity.InvestigationPeriod;
import com.ourcraft.informationcollectionsystem.entity.InvestigationPeriodPattern;
import com.ourcraft.informationcollectionsystem.service.province.investigationManage.InvestigationManageService;
import com.ourcraft.informationcollectionsystem.service.province.investigationManage.InvestigationPatternService;
import com.ourcraft.informationcollectionsystem.service.province.uploadData.UploadCollectedDataService;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityCode;
import com.ourcraft.informationcollectionsystem.utils.authority.AuthorityValidator;
import com.ourcraft.informationcollectionsystem.utils.response.Result;
import com.ourcraft.informationcollectionsystem.utils.response.ResultCode;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理调查期管理模块相关的请求
 *
 * @author Z0136
 * @since 2023-11-4
 */
@RequestMapping("province/investigationManage/")
@RestController
@CrossOrigin()
@AuthorityValidator(code = AuthorityCode.INVESTIGATION_MANAGE) // 按照‘调查期管理’权限对接口访问进行限制
public class InvestigationManageController {
    @Autowired
    private InvestigationManageService investigationManageService;
    @Autowired
    private InvestigationPatternService investigationPatternService;
    @Autowired
    private UploadCollectedDataService uploadCollectedDataService;

    /**
     * 处理‘province/investigationManage/allInvestigationPatterns’接口的前端请求，查询所有调查期模式信息
     *
     * @param request 用于权限验证
     * @return 所有调查期模式的所有属性
     */
    @GetMapping("allInvestigationPatterns")
    public Result<List<InvestigationPeriodPattern>> getAllPatterns(HttpServletRequest request) {
        return Result.success(investigationPatternService.getAllPatterns());
    }

    /**
     * 处理‘province/investigationManage/deleteInvestigationPattern’接口的前端请求，删除调查期模式
     *
     * @param patternId 需要删除的模式Id
     * @param request   用于权限验证
     * @return 是否删除成功
     */
    @DeleteMapping("deleteInvestigationPattern")
    public Result<Boolean> deletePattern(@RequestParam Integer patternId, HttpServletRequest request) {
        boolean success = investigationPatternService.deletePattern(patternId);
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/investigationManage/createInvestigationPattern’接口的前端请求，创建新调查期模式
     *
     * @param param   包含新模式的所有属性
     * @param request 用于权限验证
     * @return 是否创建成功
     */
    @PostMapping("createInvestigationPattern")
    public Result<Boolean> createPattern(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = investigationPatternService.createPattern(new InvestigationPeriodPattern(param));
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/investigationManage/modifyInvestigationPattern’接口的前端请求，修改调查期模式
     *
     * @param param   包含修改模式的id，和修改后的其他所有属性
     * @param request 用于权限验证
     * @return 是否修改成功
     */
    @PutMapping("modifyInvestigationPattern")
    public Result<Boolean> modifyPattern(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = investigationPatternService.modifyPattern(new InvestigationPeriodPattern(param));
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/investigationManage/deleteInvestigation’接口的前端请求，删除调查期
     *
     * @param investigationId 需要删除的调查期id
     * @param request         用于权限验证
     * @return 是否删除成功
     */
    @DeleteMapping("deleteInvestigation")
    public Result<Boolean> deleteInvestigation(@RequestParam Integer investigationId, HttpServletRequest request) {
        boolean success = investigationManageService.deleteInvestigation(investigationId);
        if (success) return Result.success();
        return Result.fail(ResultCode.FAIL, "已有企业提交数据，删除失败");
    }

    /**
     * 处理‘province/investigationManage/createInvestigation’接口的前端请求，创建新调查期
     *
     * @param param   包含需要创建的调查期的所有属性
     * @param request 用于权限验证
     * @return 是否创建成功
     */
    @PostMapping("createInvestigation")
    public Result<Boolean> createInvestigation(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = investigationManageService.createInvestigation(new InvestigationPeriod(param));
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/investigationManage/modifyInvestigation’接口的前端请求，修改调查期属性
     *
     * @param param   包含需要修改的调查期Id和修改后的其他属性
     * @param request 用于权限验证
     * @return 是否修改成功
     */
    @PutMapping("modifyInvestigation")
    public Result<Boolean> modifyInvestigation(@RequestBody JSONObject param, HttpServletRequest request) {
        boolean success = investigationManageService.modifyInvestigation(new InvestigationPeriod(param));
        if (success) return Result.success();
        return Result.fail();
    }

    /**
     * 处理‘province/investigationManage/allInvestigationInfo’接口的前端请求，查询所有调查期信息
     *
     * @param request 用于权限验证
     * @return 所有调查期的所有属性
     */
    @GetMapping("allInvestigationInfo")
    public Result<List<InvestigationPeriod>> getAllInvestigationInfo(HttpServletRequest request) {
        return Result.success(investigationManageService.getAllInvestigationInfo());
    }

    @PostMapping("uploadCollectedData")
    public Result<Boolean> uploadCollectedData(@RequestBody JSONObject param, HttpServletRequest request){
        return uploadCollectedDataService.upload(param.getInteger("investigationId")) ? Result.success() : Result.fail();
    }
}
